package com.tanhua.dubbo.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.UserLockMapper;
import com.tanhua.common.pojo.UserLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: UserLockService
 * @Author:王晨晗
 * @Description: 用户问卷锁状态
 * @Date: 2022/01/10/17:20
 */
@Service
public class UserLockService {
    @Autowired
    private UserLockMapper userLockMapper;

    /**
     * @description 查询用户对应问卷的锁状态
     * @param userId
     * @param questionnaireId
     * @return
     */
    public Integer getUserLock(Long userId , Long questionnaireId){
        //创建查询对象
        QueryWrapper<UserLock> wrapper = new QueryWrapper<>();
        //构建查询条件,查询指定用户的指定问卷的锁
        wrapper.eq("user_id",userId).eq("questionnaire_id" , questionnaireId);
        //执行查询,获取用户问卷锁状态
        UserLock userLock = userLockMapper.selectOne(wrapper);
        //判断是否是第一级问卷
        if(questionnaireId == 1){
            userLock.setIsLock(0);  //一级问卷默认开启
        }
        //返回用户的锁状态;
        return userLock.getIsLock();
    }

    /**
     * @description 开启问卷权限
     * @return
     */
    public void openUserLock(Long questionnaireId){
        //创建查询对象
        QueryWrapper<UserLock> queryWrapper = new QueryWrapper();
        //构建查询条件,查找特定的问卷id
        queryWrapper.eq("questionnaire_id", questionnaireId);
        //执行查询,获取用户问卷锁对象;
        UserLock userLock = userLockMapper.selectOne(queryWrapper);
        //判断用户的锁状态;为1,则开启用户当前问卷的锁
        if(userLock.getIsLock().equals(1)){
            //设置锁状态开启
            userLock.setIsLock(0);
            //执行修改操作
            userLockMapper.update(userLock,queryWrapper);
        }
    }
}
